// pages/shopManager/shopBuy/shopBuy.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nowDate: '',
        shopType: ['无', '城主', '区级代理', '市级代理', '省级代理'],
        userInfo: null,
        shopData: null,
        nowIndex: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            nowDate: new Date().getTime()
        })
        this.getUserInfo()
        this.getShopData()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    nowIndexChange: function(e) {
        this.setData({
            nowIndex: e.detail.current
        })
    },

    getUserInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res,
                    nowIndex: res.store_type > 0 ? res.store_type - 1 : res.store_type
                })
            }
        }).catch(function(e) {})
    },

    getShopData: function() {
        const that = this
        let data = {}
        common.post('Storequota/get_store_quota', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    shopData: res
                })
            }
        }).catch(function(e) {})
    },

    goMyShop: function() {
        const that = this
        if (this.data.userInfo.store_info != 3) {
            wx.navigateTo({
                url: '/pages/shopManager/shopAudit/step0/step0'
            })
        } else {
            wx.navigateToMiniProgram({
                appId: 'wxe7e8543c7f7121d8',
                path: '/pages/index/index?storeId=' + that.data.userInfo.real_store_id,
                success(res) {
                    // 打开成功
                    console.log(res)
                }
            })
        }
    },

    goBuyQuota: function() {
        wx.navigateTo({
            url: '/pages/shopManager/shopQuota/shopQuota'
        })
    },

    goApplyList: function() {
        wx.navigateTo({
            url: '/pages/shopManager/shopEmpower/shopEmpower'
        })
    },

    goShopLegal: function() {
        wx.navigateTo({
            url: '/pages/shopManager/shopLegal/shopLegal'
        })
    },

    goShopBuyDetail: function(e) {
        wx.navigateTo({
            url: '/pages/shopManager/shopBuyDetail/shopBuyDetail?id=' + e.currentTarget.dataset.id
        })
    },

    goBuy: function() {
        if (this.data.isLoading) {
            return
        }
        wx.showLoading({
            title: '请稍后'
        })
        this.setData({
            isLoading: true
        })
        const that = this
        wx.login({
            success(res) {
                if (res.code) {
                    //发起网络请求
                    let data
                    if (that.data.nowIndex == 0) {
                        data = {
                            code: res.code,
                            apply_type: 1,
                            num: 1,
                            pay_client: 1
                        }
                    } else if (that.data.nowIndex == 1) {
                        data = {
                            code: res.code,
                            apply_type: 1,
                            num: 3,
                            pay_client: 1
                        }
                    } else if (that.data.nowIndex == 2) {
                        data = {
                            code: res.code,
                            apply_type: 1,
                            num: 30,
                            pay_client: 1
                        }
                    } else if (that.data.nowIndex == 3) {
                        data = {
                            code: res.code,
                            apply_type: 1,
                            num: 100,
                            pay_client: 1
                        }
                    }
                    common.post('Storequota/apply_up_store', data).then(function(res) {
                        that.setData({
                            isLoading: false
                        })
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {

                                    }
                                }
                            })
                            return
                        }
                        if (res.type == 1) {
                            wx.showModal({
                                title: '提示',
                                content: '申请成功，请联系邀请人进行审核',
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        wx.makePhoneCall({
                                            phoneNumber: that.data.userInfo.parent_phone_c
                                        })
                                        wx.switchTab({
                                            url: '/pages/user/userCenter/userCenter'
                                        })
                                    }
                                }
                            })
                        } else {
                            wx.requestPayment({
                                timeStamp: res.timeStamp,
                                nonceStr: res.nonceStr,
                                package: res.package,
                                signType: res.signType,
                                paySign: res.paySign,
                                success(res) {
                                    if (that.data.userInfo.store_info == 3) {
                                        wx.showModal({
                                            title: '提示',
                                            content: '支付成功',
                                            showCancel: false,
                                            success(res) {
                                                if (res.confirm) {
                                                    wx.navigateBack({
                                                        delta: 1
                                                    })
                                                }
                                            }
                                        })
                                    } else {
                                        wx.showModal({
                                            title: '提示',
                                            content: '支付成功，前往完善开店资料',
                                            showCancel: false,
                                            success(res) {
                                                if (res.confirm) {
                                                    wx.redirectTo({
                                                        url: '/pages/shopManager/shopAudit/step2-2/step2-2'
                                                    })
                                                }
                                            }
                                        })
                                    }
                                },
                                fail(res) {
                                    wx.showToast({
                                        title: '支付失败',
                                        icon: 'none',
                                        duration: 1000
                                    })
                                }
                            })
                        }
                    }).catch(function(e) {})
                }
            }
        })
    }
})